import { result } from 'lodash';
/*
 * @Description:
 * @Autor: 赵子惠
 * @Date: 2021-09-06 15:05:31
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:34:13
 */
import { LazyLoadEvent } from "primeng/api";
import { Component, Injector, ViewChild } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { MessageService } from "@abp/message/message.service";
import { MoadifManageServiceProxy, StoreFunctionsServiceProxy, CollectBomServiceProxy, CollectBomManagerDto, CommonServiceProxy } from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { ResumeComponent } from "./resume/resume.component";
import { BomLookComponent } from "./bom-look/bom-look.component";

@Component({
    selector: "app-bom-manangent",
    templateUrl: "./bom-manangent.component.html",
    styleUrls: ["./bom-manangent.component.less"],
})
export class BomManangentComponent extends AppComponentBase {
    @ViewChild("showResume") ShowResume: ResumeComponent;
    @ViewChild("bomLook") BomLook: BomLookComponent;
    @ViewChild("paginator") paginator: Paginator;
    @ViewChild("dataTable") dataTable: Table;
    model = { label: undefined, value: undefined };
    createModel = undefined;
    modelOption = [];
    createModelOption = [];
    lotNo = { label: undefined, value: undefined };
    createLotNo = undefined;
    LotNoList = [];
    createLotNoList = [];
    core = undefined;
    createCore = undefined;
    coreList = [];
    createCoreList = [];
    sendTime = undefined;
    bomStuts = undefined;
    bomStutsOptions = [
        {
            label: "请选择",
            value: undefined,
        },
    ];
    rowdata: any; //子组件传参
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _collectBomServiceProxy: CollectBomServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
        //型号
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOption = result;
                this.createModelOption = result;
            })
    }
    getLotNo(event) {
          //发次
          this._commonServiceProxy.getLotNoList(
            this.model.value
           )
               .pipe(finalize(() => { })).subscribe(result => {
                   this.LotNoList = result;
                   this.createLotNoList = result;
               })
    }
    onChange(event) {
         //发次
        this._commonServiceProxy.getLotNoList(
             this.createModel.value
            )
                .pipe(finalize(() => { })).subscribe(result => {
                    this.LotNoList = result;
                    this.createLotNoList = result;
                })
    }
    add() {
        //变更申请
    }
    bomLook(row) { }
    useOrEnable(row, flag) {
        if (flag) {
            //启用
        } else {
            //停用
        }
    }
    reset() {
        this.model = undefined;
        this.core = undefined;
        this.lotNo.label = undefined;
        this.lotNo.value = undefined;
        this.sendTime = undefined;
        this.bomStuts = undefined;
        this.coreList = [];
        this.LotNoList = [];
    }
    delete(row) { }
    watch(row) {
        this.rowdata = row;
        //履历
        setTimeout(() => {
            this.BomLook.show();
            this.BomLook.getTableList();
        }, 20);
        //履历
    }
    look(row) {
        this.rowdata = row;
        setTimeout(() => {
            this.ShowResume.show();
            this.ShowResume.getTableList();
        }, 20);
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        if (this.model == undefined) {
            this.model = { label: undefined, value: undefined };
        }
        this._collectBomServiceProxy.getCollectBomContent(
            this.model.value,
            this.lotNo.value,
            this.core,
            undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    //获取左侧型号
    getModelList() {
        this._storeFunctionsServiceProxy.getRocketModeDropList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOption = result;
                this.createModelOption = result;
            })
    }
    //根据型号获取对应的发次
    getLotNoList(e, type) {
        this._collectBomServiceProxy.getSendTime(e.value)
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(v => {
                    v.value = v.label
                })
                if (type == 1) {
                    // this.createLotNo = undefined;
                    // this.createLotNoList = [];
                    this.createLotNoList = result
                } else {
                    // this.lotNo = undefined;
                    // this.LotNoList = [];
                    this.LotNoList = result
                }
            })
    }
    //根据型号发次获取对应的芯级
    getCoreList(e, type) {
        this._commonServiceProxy.getBomXj(this.model.value, this.lotNo.value, undefined, undefined,)
            .pipe(finalize(() => { })).subscribe(result => {
                this.coreList = result;
            })
    }
    //生成汇总bom的查询芯级
    getCoreListCreate(e, type) {
        this.createCoreList = [];
        this.core = undefined;
        this._commonServiceProxy.getBomXj(this.createModel.value, this.createLotNo.value, undefined, undefined,)
            .pipe(finalize(() => { })).subscribe(result => {
                this.createCoreList = result
            })
    }
    //生成汇总bom
    createBom() {
        abp.ui.setBusy();
        let dto = new CollectBomManagerDto();
        dto.module = this.model.value;
        dto.sendTime = this.lotNo.value;
        dto.xjId = this.core;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._collectBomServiceProxy.addBomCollect(
                dto
            )
                .pipe(finalize(() => { })).subscribe(result => {
                    abp.ui.clearBusy();
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('操作成功');
                        this.reset();
                        this.getTableList();
                    } else {
                        this._messageService.error(result.message)
                    }
                })
        }

    }
}
